package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {



    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult delete(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道不存在");
        }
        if (wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效，不能删除");
        }
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getChannelId,wmChannel.getId());
        queryWrapper.eq(WmNews::getStatus,WmNews.Status.PUBLISHED.getCode());
        Integer count = wmNewsMapper.selectCount(queryWrapper);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道被引用,无法删除");
        }
         wmChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateWmChannel(WmChannel wmChannel) {
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数不存在");
        }

        //判断是否被引用
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getChannelId,wmChannel.getId());
        queryWrapper.eq(WmNews::getStatus,WmNews.Status.PUBLISHED.getCode());
        Integer count = wmNewsMapper.selectCount(queryWrapper);
        if (count > 0 ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道被引用，拒绝修改");
        }

        //没有被引用
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数不存在");
        }
        Page<WmChannel> page = new Page<>(dto.getPage() == 0 ? 1:dto.getPage(),dto.getSize() == 0 ? 10 : dto.getSize());

        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(WmChannel::getName,dto.getName());
        }
        Page<WmChannel> pageResult = wmChannelMapper.selectPage(page, queryWrapper);

        ResponseResult responseResult = new PageResponseResult( dto.getPage(),dto.getSize(),(int)pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(WmChannel wmChannel) {
        if (wmChannel == null){
            log.warn("参数为null");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //去重
        LambdaQueryWrapper<WmChannel> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName,wmChannel.getName());
        Integer count = wmChannelMapper.selectCount(queryWrapper);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
        }

        //保存
        wmChannel.setCreatedTime(new Date());
        wmChannelMapper.insert(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findAll() {
        Page<WmChannel> page = new Page<>(0,100);
        Page<WmChannel> pageResult = wmChannelMapper.selectPage(page, null);
        return ResponseResult.okResult(pageResult.getRecords());
    }
}